﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using YouWei.Infrastructure.Entity;
using YouWei.Infrastructure.Repository;
using YouWei.Utility;

namespace YouWei.Op.Controllers
{
    public class HomeController : BaseController
    {
        // GET: Home
        public ActionResult Index()
        {

            //CommonEnumRepository ceRepository = new CommonEnumRepository();
            //IEnumerable<IGrouping<string, CommonEnum>> gruops = ceRepository.QueryAll().GroupBy(t => t.EnumType);
            //StringBuilder sb = new StringBuilder();
            //sb.AppendLine("using System;");
            //sb.AppendLine("");
            //sb.AppendLine("namespace ESC.Infrastructure.Enums");
            //sb.AppendLine("{");
            //foreach (IGrouping<string, CommonEnum> grp in gruops)
            //{
            //    sb.AppendLine("    public class " + grp.Key + "Enum");
            //    sb.AppendLine("    {");

            //    foreach (CommonEnum item in grp)
            //    {
            //        sb.AppendLine("        /// <summary>");
            //        sb.AppendLine("        /// " + item.EnumDesc);
            //        sb.AppendLine("        /// </summary>");
            //        sb.AppendLine("        public const int " + item.EnumName + " = " + item.EnumField + ";");
            //        sb.AppendLine("");
            //    }

            //    sb.AppendLine("        /// <summary>");
            //    sb.AppendLine("        /// 数据库枚举名称");
            //    sb.AppendLine("        /// </summary>");
            //    sb.AppendLine("        public const string EnumName = \"" + grp.Key + "\";");
            //    sb.AppendLine("");

            //    sb.AppendLine("    }");
            //    sb.AppendLine("");
            //}
            //sb.AppendLine("}");
            //string s = sb.ToString();

            return View();
        }

        /// <summary>
        /// Vin查询
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string VinQuery()
        {
            string url = "http://dpac.samr.gov.cn/web/verifyimage.create?t=" + DateTime.Now.Ticks;
            var resut = HttpWebRequestService.GetRequestImg(url, "", "image/webp,image/apng,image/*,*/*;q=0.8");
            Dictionary<string, string> cookieData = new Dictionary<string, string>();
            var cookies = ControllerContext.HttpContext.Request.Cookies;
            for (int i = 0; i < cookies.Count; i++)
            {
                cookieData.Add(cookies[i].Name,cookies[i].Value);
            }

            string vin = GetParam("vin");
            string veryCode = GetParam("veryCode");
            Dictionary<string, string> postData = new Dictionary<string, string>();
            postData.Add("vin", vin);
          return  HttpWebRequestService.PostRequest("http://dpac.samr.gov.cn/web/vinQuery/vinQuery.do?m=queryVin", postData, "application/x-www-form-urlencoded", cookieData);
        }
    }
}